package com.bdqn.business.academic.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.business.academic.dao.CurriculumMapper;
import com.bdqn.business.academic.dao.SchoolMapper;
import com.bdqn.entity.academic.Curriculum;
import com.bdqn.entity.academic.School;
import com.bdqn.response.PageResult;
import com.bdqn.response.Result;
import com.bdqn.response.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
@Transactional
public class CurriculumService {

    @Autowired
    private CurriculumMapper curriculumMapper;

    @Autowired
    private SchoolMapper schoolMapper;

    public Result getList(Map<String, Object> map) {
        IPage<Curriculum> iPage = new Page<>(Integer.parseInt(map.get("page").toString()),Integer.parseInt(map.get("size").toString()));
        QueryWrapper<Curriculum> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("school_id");
        queryWrapper.orderByDesc("sort_number");
        IPage<Curriculum> result = curriculumMapper.selectPage(iPage, queryWrapper);
        return new Result(ResultCode.SUCCESS,new PageResult<Curriculum>(result.getTotal(),result.getRecords()));
    }

    @CachePut(cacheNames = "curriculum",key = "#curriculum.id")
    public Result save(Curriculum curriculum) {

        School school = schoolMapper.selectById(curriculum.getSchoolId());
        curriculum.setSchoolName(school.getName());
        curriculumMapper.insert(curriculum);

        return new Result(ResultCode.SUCCESS,curriculum);
    }
}
